package Prototype;

public class Email implements Cloneable {
    //附件
    private Attachment attachment;

    public Email() {
        this.attachment = new Attachment();
        System.out.println("原邮件的:attachment:"+attachment);
    }
    //获取附件
    public Attachment getAttachment() {
        return attachment;
    }
    //设置附件
    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }
    //浅克隆
    public Object clone(){
//        System.out.println("浅克隆:"+super.clone());
//        return super.clone(); //调用这个方法实现浅克隆(只赋值引用)
        try {
            System.out.println("浅克隆的Email:"+super.clone());
            System.out.println("浅克隆的attachment:"+attachment);
            return super.clone();  // 只是复制 Email 本身，Attachment 没有复制
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }
    //深拷贝
    // 深克隆
    public Object deepClone() {
        Email copy = null;
        try {
            copy = (Email) super.clone();  // 先复制 Email
            // 调用 attachment 自己的 clone()
            copy.attachment = (Attachment) attachment.clone();// 再复制 Attachment
            System.out.println("深克隆:"+copy);
            System.out.println("深克隆:"+copy.attachment);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return copy;
    }
    //显示邮件
    public void show(){
        System.out.println("显示邮件");
    }
}
